package cn.zysuyuan.item.controller;

import cn.zysuyuan.common.entity.R;
import cn.zysuyuan.item.service.PermissionService;
import cn.zysuyuan.item.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-18 15:19
 */
// 角色分配
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分配角色
     */
    @PutMapping("/user/assignRoles")
    public R save(@RequestBody Map<String,Object> map) {
        // 角色分配
        userService.assignRoles(map);
        return R.ok();
    }

    /**
     * 查询拥有的权限
     * @param level 用户级别
     * @return
     */
    @GetMapping("/user/selectRoles")
    public Map<String,Object> findRoles(@RequestParam("level") String level,@RequestParam("id") String id) {
        Map<String,Object> map = new HashMap<>();
        if ("admin".equals(level)) {
            map = userService.findAll();
        } else{
            map = userService.findRoles(id);
        }
        return map;
    }

    @GetMapping("/user/rolelist")
    public R getRoleList(@RequestParam("id") String id) {
        Map<String,Object> map = new HashMap<>();
        map = userService.getRoleList(id);
        return R.ok(map);
    }

}
